package com.teach.servlet;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.teach.entity.user;
import com.teach.service.UserService;
import com.teach.service.impl.UserServiceImpl;

public class UserAddServlet extends HttpServlet{

	private UserService userservice=new UserServiceImpl();
	SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd");
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		user u=new user();
		
		u.setName(request.getParameter("userName"));
		u.setPassword(request.getParameter("userPassword"));
		u.setNickname(request.getParameter("userNickname"));
		u.setAccount_number(request.getParameter("account_number"));
		u.setBirthday(Date.valueOf(request.getParameter("userBrithdate")));
//		u.setCreate_time(Date.valueOf(df.format(new java.util.Date())));
		u.setUser_address(request.getParameter("userAddress"));
		u.setPhone(request.getParameter("userPhone"));
		
		
		try{//需要非String类型的时候，需要进行类型的转换
			String userSex = request.getParameter("userSex");
			String sex=(Boolean.valueOf(userSex))?"男":"女";
			u.setSex(sex);
		}catch(Exception ex){}
		
		System.out.println(u);
		try{
			userservice.add(u);
			request.setAttribute("msg", "添加用户成功");
			request.setAttribute("href","user.let");
		}catch(Exception ex){
			ex.printStackTrace();//记调试日志
			request.setAttribute("error", "添加用户失败:"+ex.getMessage());
		}
		//跳转到视图
		request.getRequestDispatcher("/admin/msg.jsp").forward(request, response);
	}
}
